<script setup>
import { ref, nextTick, onMounted } from "vue";
// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
// 引入折线图图表，图表后缀都为 Chart
import { BarChart } from "echarts/charts";
// 引入提示框，标题，直角坐标系，数据集，内置数据转换器组件，组件后缀都为 Component
import {
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
} from "echarts/components";
// 标签自动布局、全局过渡动画等特性
import { UniversalTransition } from "echarts/features";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";

// 注册必须的组件
echarts.use([
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  BarChart,
  UniversalTransition,
  CanvasRenderer,
]);

// 接收数据
const props = defineProps({
  amount: {
    type: Array,
    default: function () {
      return [];
    },
  },
});

let entries = [];
let values = [];
const initData = () => {
  for (let i = 0; i < props.amount.length; i++) {
    entries.push(props.amount[i].time);
    values.push(props.amount[i].value);
  }
};
initData();

// 初始化图表
const optionForm = {
  // title: {
  //   text: "24h水量",
  //   textStyle: {
  //     color: "rgba(241, 241, 241, 1)",
  //     fontStyle: "italic",
  //     fontWeight: "lighter",
  //     fontSize: 17,
  //   },
  //   left: "50%",
  //   textAlign: "center",
  // },
  color: ["#3398DB"],
  tooltip: {
    trigger: "axis",
    axisPointer: {
      type: "shadow",
    },
  },
  grid: {
    left: "10%",
    right: "3%",
    top: "15%",
    bottom: "22%",
  },
  xAxis: [
    {
      type: "category",
      data: [],
      nameTextStyle: {
        color: null,
      },
      axisLine: {
        show: true,
        onZero: true,
        lineStyle: {
          color: "rgba(255, 255, 255, 1)",
        },
      },
      axisLabel: {
        rotate: 70,
      },
    },
  ],
  yAxis: [
    {
      type: "value",
      axisLine: {
        lineStyle: {
          color: "rgba(255, 255, 255, 1)",
        },
      },
    },
  ],
  series: [
    {
      type: "bar",
      barWidth: "60%",
      data: [],
      showBackground: true,
      backgroundStyle: {
        color: "rgba(180, 180, 180, 0.2)",
      },
      label: {
        show: true,
      },
      itemStyle: {
        color: "rgba(38, 144, 243, 1)",
      },
    },
  ],
};
const option = ref({ ...optionForm });

let containerRef = ref(null);
const initChart = () => {
  // 接下来的使用就跟之前一样，初始化图表，设置配置项
  const myChart = echarts.init(containerRef.value);
  // 加数据
  option.value.xAxis[0].data = entries;
  option.value.series[0].data = values;
  myChart.setOption(option.value);
};

onMounted(() => {
  nextTick(() => {
    initChart();
  });
});
</script>

<template>
  <div ref="containerRef" id="container"></div>
</template>

<style lang="scss" scoped>
#container {
  height: 100%;
  width: 100%;
}
</style>